<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客登陆页</title>

    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/login.css">

</head>

<body>
    <div class="nav">
        <img src="pic/logo2.jpg" alt="">
        <span class="blog-title">我的博客系统</span>
        <div class="space"></div>
        <a class="nav-span" href="blog_list.html">主页</a>
        <a class="nav-span" href="blog_edit.html">写博客</a>
    </div>

    <div class="container-login">
        <div class="login-dialog">
            <h3>登陆</h3>
            <div class="row">
                <span>用户名</span>
                <input type="text" name="username" id="username">
            </div>
            <div class="row">
                <span>密码</span>
                <input type="password" name="password" id="password">
            </div>
            <div class="row">
                <button id="submit" onclick="login()">提交</button>
            </div>
            <div class="row">
                <span>还没账号? 点此<a href="blog_register.html">注册</a></span>
            </div>
            
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script src="js/common.js"></script>
    <script>
        // 检测jQuery是否正确加载
        if (typeof jQuery === 'undefined') {
            console.error('jQuery未加载！使用原生方法');
            document.getElementById('submit').onclick = function() {
                loginWithFetch();
            };
        }
        
        // 原生Fetch API的登录函数，作为备份
        function loginWithFetch() {
            var username = document.getElementById('username').value.trim();
            var password = document.getElementById('password').value.trim();
            
            if (!username || !password) {
                alert("用户名和密码不能为空");
                return;
            }
            
            document.getElementById('submit').disabled = true;
            
            fetch("http://127.0.0.1:10030/user/login", {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    "userName": username,
                    "password": password
                })
            })
            .then(response => response.json())
            .then(result => {
                if (result.code == 200 && result.data != "") {
                    var response = result.data;
                    localStorage.setItem("user_token", response.token);
                    localStorage.setItem("loginUserId", response.userId);
                    window.location.href = "blog_list.html";
                } else {
                    alert("用户名或密码错误");
                    document.getElementById('submit').disabled = false;
                }
            })
            .catch(error => {
                console.error("登录失败:", error);
                alert("登录失败: " + error.message);
                document.getElementById('submit').disabled = false;
            });
        }
        
        function login() {
            // 禁用按钮，防止重复点击
            $("#submit").prop("disabled", true);
            
            // 获取输入值
            var username = $("#username").val().trim();
            var password = $("#password").val().trim();
            
            // 简单校验
            if(!username || !password) {
                alert("用户名和密码不能为空");
                $("#submit").prop("disabled", false);
                return;
            }
            
            // 使用common.js中定义的baseUrl
            $.ajax({
                type: "post",
                url: baseUrl + "/user/login",
                contentType: "application/json",
                data: JSON.stringify({
                    "userName": username,
                    "password": password
                }),
                success: function (result) {   
                    if (result.code == 200 && result.data != "") {
                        var response = result.data;
                        localStorage.setItem("user_token", response.token);
                        localStorage.setItem("loginUserId", response.userId);
                        location.assign("blog_list.html");
                    } else {
                        alert("用户名或密码错误");
                        $("#submit").prop("disabled", false);
                    }
                },
                error: function(xhr, status, error) {
                    console.error("登录失败:", error);
                    if(xhr.status === 0) {
                        alert("无法连接到服务器，请检查网络连接或服务器是否启动");
                    } else {
                        alert("登录失败: " + (xhr.responseJSON?.errMsg || error || "未知错误"));
                    }
                    $("#submit").prop("disabled", false);
                }
            });
        }
        
        // 回车键触发登录
        $(document).ready(function() {
            $("#username, #password").keypress(function(e) {
                if(e.which === 13) { // Enter键
                    login();
                }
            });
        });
    </script>
</body>

</html>